<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="form" :model="queryParams" :inline="true">
        <el-form-item prop="keywords" label="流程任务名称">
          <el-input
            v-model="queryParams.keywords"
            placeholder="流程任务名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery" icon="el-icon-search">搜索</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <template #header>
        <!-- <el-button type="success" @click="openDialog()"><i class="el-icon-circle-plus"></i> 新增</el-button>
        <el-button type="danger" :disabled="ids.length === 0" @click="handleDelete()"><i class="el-icon-delete"></i> 删除</el-button> -->
      </template>

      <el-table
        v-loading="loading"
        :data="dataList"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" width="55" prop="dataIndex"/>
        <el-table-column label="流程实例ID" prop="processInstanceId"/>
        <el-table-column label="任务名称" prop="name"/>
        <el-table-column label="业务KEY" prop="businessKey"/>
        <el-table-column fixed="right" label="操作"  min-width="280" width="300">
          <template #default="scope">
            <el-button type="warning" size="small" link @click="showProcessImage(scope.row.id)" ><i class="el-icon-picture-outline"></i> 流程图</el-button>
            <el-button type="success" size="small" link @click="claim(scope.row.id)" ><i class="el-icon-picture-outline"></i> 任务拾取</el-button>
            <el-button type="danger" size="small" link @click="handleDelete(scope.row.id)"><i class="el-icon-delete"></i> 删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.pageNum"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>

    <el-dialog :visible.sync="showProcessImageVisible" title="实时流程图">
      <div style="height: 400px;">
        <el-image :src="jpgUrl" fit="fill"></el-image>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import processTaskApi from '@/api/workflow/processTask'
import { formatDate } from '@/utils/formatTime'
export default {
	name: 'processDefinitionIndex',
	data() {
		return {
			queryParams: {
        keywords: '',
        pageNum: 0,
        pageSize: 10,

      },
      total: 0,
      loading: false,
      dataList: [],
      ids: [],
      dialog: {},
      formData: {},
      rules: {},

      showProcessImageVisible: false,
      jpgUrl: '',

		};
	},
  methods: {
    claim (id) {
      processTaskApi.claim(id).then((res) => {
        this.$message.success('任务拾取成功');
        this.handleQuery();
      });
    },
    showProcessImage (id) {
      this.showProcessImageVisible = true;
      this.loading = true;
      processTaskApi.image(id).then((res) => {
        const binaryData = [];
        binaryData.push(res);
        let blob = new Blob(binaryData, { type: 'image/jpeg' });
        this.jpgUrl = window.URL.createObjectURL(blob);
        this.loading = false;
      });
    },
    handleQuery() {
      this.loading = true;
      let params = {
        number: this.queryParams.pageNum,
        size: this.queryParams.pageSize
      };
      processTaskApi.roleTodo(params).then((res) => {
        let data = res.data;
        let that = this;
        let index = 1
        data.content.forEach((v) => {
          v.dataIndex = index++;
        });
        this.dataList = data.content;
        
        this.total = data.count;
        this.loading = false;
      }).catch(() => {});
    },
    resetQuery() {
      this.queryParams = {
        keywords: '',
        pageNum: 0,
        pageSize: 10,
      };
      this.handleQuery();
    },
    addOrUpdate() {
      
    },
    openDialog(id) {
      let baseUrl = process.env.CLOUD_SERVICE_ACTIVITI_BASE_URL;
      if (!baseUrl) {
        baseUrl = 'http://192.168.10.29:30003'
      }
      let url = baseUrl + '/activiti-explorer/service/create';
      if (id) {
        url = baseUrl + '/modeler.html?modelId=' + id;
      }
      window.open(url, '_blank');
    },
    closeDialog() {
    },
    handleDelete(id) {
      if (id) {
        dictApi.delete(id).then((res) => {
          this.$message({message: '字典数据删除成功！', type: 'success'});
          this.handleQuery();
        });
      } else {
        this.$message.error('暂不支持批量删除字典数据！');
      }
    },
    handleSelectionChange(val) {
      this.ids = [];
      val.forEach(v => {
        this.ids.push(v.id);
      });
    },
    handleSelectionRoleChange(rows) {
      if (rows) {
        let arr = [];
        rows.forEach((v) => {
          arr.push(v.id);
        });
        this.selectRoleIdArr = arr;
      }
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.handleQuery();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.handleQuery();
    },
  },
  created() {
    this.handleQuery();
  }
};
</script>

<style scoped>
.el-pagination {
  margin-top: 10px;
  float: right;
  margin-bottom: 10px;
}
</style>